﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Advert
{
    public partial class edit : PageBase
    {
        string AdvertId = string.Empty;
        string PicId = string.Empty;

        BLL.AdvertPics bll = new BLL.AdvertPics();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("advert", "modify");

            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                AdvertId = Request.QueryString["id"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pid"]))
            {
                PicId = Request.QueryString["pid"];
            }
            if (!IsPostBack)
            {
                BindData();
                BindPic();
            }
        }

        private void BindData()
        {
            string strWhere = "IsActive=1 and AdvertId=" + AdvertId;
            
            int recordCount = bll.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = bll.GetListByPage(strWhere, "OrderNo asc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);

            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        private void BindPic()
        {
            if (!string.IsNullOrEmpty(PicId))
            {
                Model.AdvertPics model = bll.GetModel(int.Parse(PicId));
                txtTitle.Text = model.Title;
                txtUrl.Text = model.Url;
                txtOrderNo.Text = model.OrderNo.ToString();
                if (!string.IsNullOrEmpty(model.Pic))
                {
                    Image1.ImageUrl = "~/uploadfiles/ads/" + model.Pic;
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Model.AdvertPics model = bll.GetModel(int.Parse(PicId));
            model.Title = txtTitle.Text;
            model.Url = txtUrl.Text;
            model.OrderNo = int.Parse(txtOrderNo.Text);
            ImageUpload img = new ImageUpload();
            img.SavePath = "~/uploadfiles/ads/";
            img.FormFile = File1;
            img.Upload();
            if (img.Error == 0)
            {
                model.Pic = img.OutFileName;
            }
            model.UpdateTime = DateTime.Now;

            bll.Update(model);

            Response.Redirect("edit.aspx?id=" + AdvertId);
        }
    }
}